﻿//工作量化表格
var workStatsManage = {
    //员工部门量化类型:1--员工量化,2--部门量化
    dataType: 1,
    departmentKeyId: "",
    userKeyId: '',
    sStartDate: "",
    sEndDate: "",
    //1表示日度数据，2表示周度数据，3表示月度数据
    dateType: 1,
    //是否显示单条数据
    isShowSingle: 1,
    init: function () {
        var me = this;
        var dataType = $("#hiddataType").val();
        var departmentKeyId = $("#hiddepartmentKeyId").val();
        var userKeyId = $("#hiduserKeyId").val();
        var sStartDate = $("#dStartDate").val();
        var sEndDate = $("#dEndDate").val();
        if (dataType) {
            me.dataType = dataType;
        }
        me.sStartDate = sStartDate;
        me.sEndDate = sEndDate;
        me.userKeyId = userKeyId;
        me.departLevel = $('#hDepartLink a[stype=self]').attr('level');
        me.departmentKeyId = departmentKeyId;
        me.isShowSingle = 1;
        me.ajaxDataList();
        me.initEvent();
    },
    undefinedOrNull: function (property) {
        //处理undefined或null
        if (typeof property == 'undefined' || property == null || property == 'NULL') {
            return '';
        } else {
            return property;
        }
    },
    initEvent: function () {
        var me = this;
        //部门员工标签切换
        $("#ulwqtype input").click(function () {
            $('#ulwqtype input').removeAttr("checked");
            $(this).attr("checked", "checked");
            var dataType = $(this).attr('stype');
            if (dataType == "department") {
                dataType = 2;
            } else {
                dataType = 1;
            }
            me.dataType = dataType;
            me.ajaxDataList();
        });
        //绑定部门面包屑链接事件
        me.bindHeadDepartLinkEvent();
        // 绑定部门列td点击事件, 异步加载该部门下的信息
        me.bindDepartTdClickEvent();
        //绑定量化指标值td点击事件,显示详情
        me.bindIndexValueTdClickEvent();
    },
    //绑定部门面包屑链接事件
    bindHeadDepartLinkEvent: function () {
        var me = this;
        $('#hDepartLink').on('click', 'a', function () {
            $(".quantization-tables .portal-load").show();
            $(this).nextAll().not("div").remove();
            var departmentKeyId = $(this).attr('did');
            var departLevel = $(this).attr('level');
            var stype = $(this).attr('stype');
            var maxLevel = $(this).attr('max_level');
            me.setTabHideShowByLevel(departLevel, maxLevel);
            departLevel = parseInt(departLevel);
            var dataType = 2;
            if (departLevel == maxLevel) {
                dataType = 1;
            }
            me.dataType = dataType;
            me.departLevel = departLevel;
            me.departmentKeyId = departmentKeyId;
            $('#divDataTable').show();
            if (stype == 'self') {
                me.dataType = $('#hiddataType').val();
                me.isShowSingle = 1;
                //当点击自身时部门员工切换隐藏
                $('#ulwqtype').hide();
            } else {
                me.isShowSingle = 0;
            }
            me.ajaxDataList();
            $("#hDepartLink").find("a").last().css({ "color": "#000", "cursor": "default" }).siblings().not("div").css({ "color": "#428bca", "cursor": "pointer" });
            $("#liwqtype_depart").attr("checked", "checked");
            $("#liwqtype_staff").removeAttr("checked");
        });
    },
    // 绑定部门列td点击事件, 异步加载该部门下的信息
    bindDepartTdClickEvent: function () {
        var me = this;
        $('#divDataContainer').on('click', 'td[stype=depart]', function () {
            other.showLoaderMask();
            $(".quantization-tables .portal-load").show();
            var departmentKeyId = this.id.replace('tdwq_', '');
            var departLevel = $(this).attr('level');
            var dataType = 2;
            var maxLevel = $(this).attr('max_level');
            me.setTabHideShowByLevel(departLevel, maxLevel);
            if (departLevel == maxLevel) {
                dataType = 1;
            }
            me.dataType = dataType;
            me.isShowSingle = 0;
            var departmentName = $(this).attr("title");
            departLevel = parseInt(departLevel);
            me.departLevel = departLevel;
            me.departmentKeyId = departmentKeyId;
            $("#hDepartLink").append('<a href="javascript:void(0);" did="' + departmentKeyId + '" max_level="' + maxLevel + '" level="' + departLevel + '"> / ' + departmentName + '</a>');
            me.ajaxDataList();

            $("#hDepartLink").find("a").last().css({ "color": "#000", "cursor": "default" }).siblings().not("div").css({ "color": "#428bca", "cursor": "pointer" });

        });
    },
    //设置部门员工切换标签显示与隐藏
    setTabHideShowByLevel: function (departLevel, maxLevel) {
        $('#ulwqtype').hide();
        if (maxLevel - departLevel != 0) {
            $('#ulwqtype').show();
        }
    },
    ajaxDataList: function () {
        var me = workStatsManage;
        var params = {
            departmentKeyId: me.departmentKeyId,
            dataType: me.dataType,
            sStartDate: me.sStartDate,
            sEndDate: me.sEndDate,
            userKeyId: me.userKeyId,
            dateType: me.dateType,
            isShowSingleStaff: me.isShowSingle,
            rd: Math.random()
        };
        $.ajax({
            data: params,
            url: '/portal/get-workStats',
            dataType: 'JSON',
            cache: false,
            type: 'post',
            success: function (data) {
                if (data) {
                    data = JSON.parse(data);
                    if (data.responseContent) {
                        var dataType = data.responseContent.dataType;
                        if (dataType && dataType != me.dataType) {
                            me.dataType = dataType;
                            $('#ulwqtype').hide();
                        }
                        $('#divNoWorkStats').hide();
                        me.bindWorkStatsTable(data);
                    } else {
                        if (data.errorMsg) {
                            $("#divNoWorkStats").html(data.errorMsg);
                        }
                        me.onNoWorkStats();
                    }
                } else {
                    me.onNoWorkStats();
                }
            },
            error: function () {
            },
            complete: function () {
                $('#divDataContainer').show();
            }
        });
    },
    //绑定量化指标值td点击事件,显示详情
    bindIndexValueTdClickEvent: function () {
        var me = workStatsManage;
        $('#divDataContainer').on('click', 'td[bhasdetail=1]', function () {
            var detailCount = $(this).html();
            if (detailCount == "0") {
                return false;
            }
            other.showLoaderMask();
            $(".quantization-tables .portal-load").show();
            var detailName = $(this).attr('detailName');
            var departmentNo = $(this).parent().attr('departmentNo');
            var departmentKeyId = $(this).parent().attr('departmentKeyId');
            var userKeyId = $(this).parent().attr('userKeyId');
            var indexCode = $(this).attr("indexCode");
            var params = {
                indexCode: indexCode,
                dataType: me.dataType,
                departmentKeyId: departmentKeyId,
                sStartDate: me.sStartDate,
                sEndDate: me.sEndDate,
                userKeyId: userKeyId,
                departmentNo: departmentNo,
                rd: Math.random()
            };
            $.ajax({
                data: params,
                url: '/portal/get-workStats-details',
                dataType: 'JSON',
                cache: false,
                type: 'post',
                success: function (data) {
                    if (data) {
                        data = JSON.parse(data);
                        var detailsTbodyHtml = "";
                        if (data.responseContent && data.responseContent.details && data.responseContent.details.length > 0) {
                            $.each(data.responseContent.details, function (i, item) {
                                detailsTbodyHtml += '<tr><td class="tl"><div style="height:44px;line-height:44px;overflow:hidden;max-width:200px;" title="' + item.position + '">' + item.position + '</div></td><td><a target="_blank" href="/property/details?keyId=' + item.propertyKeyId + '">' + item.houseNo + '</a></td><td>' + item.employeeName + '</td><td>' + item.departmentName + '</td></tr>';
                            });

                        } else {
                            detailsTbodyHtml = '<tr><td colspan="4" class="bg-warning">' + data.errorMsg + '</td></tr>';
                        }
                        $('#basicTable tbody').html(detailsTbodyHtml);
                        other.hideLoaderMask();
                        $(".quantization-tables .portal-load").hide();
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert('error ' + textStatus + " " + errorThrown);
                },
                complete: function () {
                    $.dialog({
                        id: "basicTablePop",
                        lock: true,
                        width: 700,
                        title: detailName + "详情",
                        content: $('#basicTablePop').html()
                    });
                }
            });
            $("#detailCountRefer").hide();
            if (Number($(this).html()) > 10) {
                $("#detailCountRefer").show();
            }
            //跟据弹框的不同显示不同字段
            if (detailName == "实勘") {
                $(".person-type").text("实勘");
            } else if (detailName == "钥匙") {
                $(".person-type").text("收钥");
            }
            else if (detailName == "押房") {
                $(".person-type").text("押房");
            }
            else if (detailName == "签约" || detailName == "委托房") {
                $(".person-type").text("签署");
            } else {
                $(".person-type").text("开盘");
            }

        });
    },
    //日期控件值的工作量化周期改变后
    datePickedFunc: function () {
        timehelper.changeTimeInput($("#dStartDate"), $("#dEndDate"));
        $(".quantization-tables .portal-load").show();
        var me = workStatsManage;
        var sStartDate = $('#dStartDate').val();
        var sEndDate = $('#dEndDate').val();
        me.sStartDate = sStartDate;
        me.sEndDate = sEndDate;
        //限定一个月的查询
        var dateDiff = (timehelper.TimeDiffirence(me.sStartDate, me.sEndDate).msDiff) / 86400000;
        if (dateDiff > $("#dateDiff").val()) {
            $.OKDialogAgency('<div class="tc">为了保证查询速度，请查询少于30天的数据！</div>');
            $(".quantization-tables .portal-load").hide();
            return false;
        }
        me.ajaxDataList();
    },
    bindWorkStatsTable: function (data) {
        var me = this;
        if (data && (data.responseContent.workStatses && data.responseContent.workStatses.length > 0)) {
            var fixTbodyHtml = "",
                floatTbodyHtml = "",
                employeeLen = data.responseContent.workStatses.length;
            $.each(data.responseContent.workStatses, function (i, item) {
                if (me.dataType == 2) {
                    fixTbodyHtml += '<tr><td title=' + item.departmentName + ' id="tdwq_' + item.departmentKeyId + '" max_level="' + data.responseContent.maxLevel + '" stype="depart" level="' + item.level + '" style="cursor: pointer;"><div>' + item.departmentName + '</div></td></tr>';
                    floatTbodyHtml += '<tr departmentNo="' + item.departmentNo + '"  departmentKeyId="' + item.departmentKeyId + '">';
                    $('#employeeLen').html('');
                } else {
                    $('#employeeLen').html('(' + employeeLen + ')');
                    fixTbodyHtml += '<tr><td id="tdwq_' + item.userKeyId + '" title="' + item.employeeName + '" stype="staff"><div>' + item.employeeName + '</div></td></tr>';
                    floatTbodyHtml += '<tr departmentNo="' + item.departmentNo + '"  departmentKeyId="' + item.departmentKeyId + '"  userKeyId="' + item.userKeyId + '">';
                }
                floatTbodyHtml += '<td bhasdetail="1" detailName="售房" indexCode="11">' + me.undefinedOrNull(item.propertysCountSaleSum) + '</td>';
                floatTbodyHtml += '<td bhasdetail="1" detailName="租房" indexCode="12">' + me.undefinedOrNull(item.propertysCountRentSum) + '</td>';
                floatTbodyHtml += '<td>' + me.undefinedOrNull(item.inquirysCountSaleSum) + '</td>';
                floatTbodyHtml += '<td>' + me.undefinedOrNull(item.inquirysCountRentSum) + '</td>';
                floatTbodyHtml += '<td>' + me.undefinedOrNull(item.propertyFllow) + '</td>';
                floatTbodyHtml += '<td>' + me.undefinedOrNull(item.inquiryFllow) + '</td>';
                floatTbodyHtml += '<td>' + me.undefinedOrNull(item.takeSeeSale) + '</td>';
                floatTbodyHtml += '<td>' + me.undefinedOrNull(item.takeSeeRent) + '</td>';
                floatTbodyHtml += '<td bhasdetail="1" detailName="钥匙" indexCode="31">' + me.undefinedOrNull(item.keysRent) + '</td>';
                floatTbodyHtml += '<td bhasdetail="1" detailName="押房" indexCode="43">' + me.undefinedOrNull(item.exclusiveEntrustCount) + '</td>';
                floatTbodyHtml += '<td bhasdetail="1" detailName="签约" indexCode="32">' + me.undefinedOrNull(item.exclusive) + '</td>';
                floatTbodyHtml += '<td bhasdetail="1" detailName="实勘" indexCode="33">' + me.undefinedOrNull(item.realSurvey) + '</td>';
                floatTbodyHtml += '<td>' + me.undefinedOrNull(item.changeCount) + '</td>';
                floatTbodyHtml += '<td>' + me.undefinedOrNull(item.browsePhoneCount) + '</td>';
                floatTbodyHtml += '<td>' + me.undefinedOrNull(item.dragInquirysCountSale) + '</td>';
                floatTbodyHtml += '<td>' + me.undefinedOrNull(item.dragInquirysCountRent) + '</td>';
                floatTbodyHtml += '<td>' + me.undefinedOrNull(item.virtualNumberCount) + '</td>';
                floatTbodyHtml += '<td>' + me.undefinedOrNull(item.virtualNumberConnect) + '</td>';
                floatTbodyHtml += '<td>' + me.undefinedOrNull(item.channelInquiryCount) + '</td>';
                floatTbodyHtml += '<td bhasdetail="1" detailName="委托房" indexCode="48">' + me.undefinedOrNull(item.entrustPro) + '</td></tr>';
            });
            $('#divNoWorkStats').hide();
            $('#tableFix tbody').html(fixTbodyHtml);
            $('#tableFloat tbody').html(floatTbodyHtml);
            $(".quantization-tables .portal-load").hide();
            other.hideLoaderMask();
            //为非0单元格增加手型样式属性
            $("#tableFloat").find("td").each(function () {
                var $this = $(this);
                if (Number($this.html()) > 0 && $(this).attr('bhasdetail') == '1') {
                    $this.css({ "cursor": "pointer", "background-color": "#fbfaf7" });
                }
            });
        } else {
            me.onNoWorkStats();
        }
        var hDivDataTable = $("#divDataTable").height();
        if (hDivDataTable > 500) {
            $("#divDataTable").find("th").css("visibility", "hidden");
            $(".fixed-div-data-table").show();
        } else {
            $("#divDataTable").find("th").css("visibility", "visible");
            $(".fixed-div-data-table").hide();
        }
    },
    onNoWorkStats: function () {
        $('#divDataTable tbody').empty();
        $('#divNoWorkStats').show();
        $(".quantization-tables .portal-load").hide();
        other.hideLoaderMask();
    }
};

//日期格式转换   
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(), //day
        "h+": this.getHours(), //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
            RegExp.$1.length == 1 ? o[k] :
                ("00" + o[k]).substr(("" + o[k]).length));
    return format;
};

workStatsManage.init();